package pl.msikora.android.lapmagic.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class LapsTable {
	
	private static final String TAG = "LapsTable";
	
	public static final String TABLE_LAPS = "laps";
	public static final String COLUMN_SESSION_ID = "session_id";
	public static final String COLUMN_LAP_NO = "lap_no";
	public static final String COLUMN_LAP_TIME = "lap_time";
	public static final String COLUMN_TOP_SPEED = "top_speed";
	
	private static final String TABLE_CREATE = "create table "
			+ TABLE_LAPS
			+ "(" 
			+ COLUMN_SESSION_ID + " integer not null, " // references sessions on delete cascade
			+ COLUMN_LAP_NO + " integer not null, "
			+ COLUMN_LAP_TIME + " integer not null, " // in milliseconds
			+ COLUMN_TOP_SPEED + " integer not null, "
			+ "primary key(" + COLUMN_SESSION_ID + ", " + COLUMN_LAP_NO + ")"
			+ ");";
	
	public static void onCreate(SQLiteDatabase database) {
		Log.d(TAG, "creating table laps with statement '" + TABLE_CREATE
				+ "'");
		database.execSQL(TABLE_CREATE);
	}
	
	public static void onUpgrade(SQLiteDatabase database, int oldVersion, 
			int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion
				+ " to " + newVersion + ", which will destroy new all old data");
		database.execSQL("drop table if exists " + TABLE_LAPS);
		onCreate(database);
	}

}
